package com.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: AaronChun
 * @create: 2019-03-28 16:40
 * @description:
 **/
public class CglibDelegate implements MethodInterceptor {


    public Object getInstance(Class clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);


        return enhancer.create();

    }



    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("年轻人，一定要注意身体，戒骄戒躁");


        try {


            return methodProxy.invokeSuper(obj, args);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            System.out.println("认识到自己是平凡且普通的，坚持做一件普通的事情，一直做。");
        }

        return null;
    }
}
